package CCRMClient.GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;
import Common.Entities.CustomerResponse;

/**
 * this class implements the add customer response screen
 * @author Ola
 *
 */
public class CustomerResponseScreen extends JPanel {
  
	
	private JLabel lblCustomerResponse;
	private JLabel lblResponseType;
	private JLabel lblrefusalDescription;
	private JLabel lblDuration;
	private JLabel lblCustID;
	private JLabel lblProdID;
	private JLabel lblSaleID;
	private JTextField textResponseType;
	private JTextField textrefusalDescription;
	private JTextField textDuration;
	private JTextField textCustID;
	private JTextField textProdID;
	private JTextField textSaleID;
	private JButton btnGoBack;
	private JButton btnUpdate;
	
	/**
	 * Create the panel.
	 */
	public CustomerResponseScreen(String saleID, String customerID) {
		initialize();
	    String SaleID = saleID;
	    textSaleID.setText(SaleID);
	    textSaleID.setEditable(false);
	    String CustomerID = customerID;
	    textCustID.setText(CustomerID);
	    textCustID.setEditable(false);
	}
	private void initialize(){
		setSize (507,430);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblCustomerResponse());
		add(getLblResponseType());
		add(getLblrefusalDescription());
		add(getTextResponseType());
		add(getTextrefusalDescription());
		add(getLblDuration());
		add(getLblCustID());
		add(getLblSaleID());
		add(getTextDuration());
		add(getTextSaleID());
		add(getTextCustID());
		add(getBtnGoBack());
		add(getBtnUpdate());
		add(getLblProdID());
		add(getTextProdID());
	}
	
	JLabel getLblCustomerResponse(){
		if(lblCustomerResponse==null){
			lblCustomerResponse=new JLabel("Customer Response");
			lblCustomerResponse.setHorizontalAlignment(SwingConstants.CENTER);
			lblCustomerResponse.setForeground(Color.RED);
			lblCustomerResponse.setFont(new Font("David", Font.BOLD, 26));
			lblCustomerResponse.setBackground(Color.WHITE);
			lblCustomerResponse.setBounds(0, 0, 507, 39);
		}
		return lblCustomerResponse;
	}
	
	JLabel getLblResponseType(){
		if(lblResponseType==null){
			lblResponseType=new JLabel("Response Type :");
			lblResponseType.setHorizontalAlignment(SwingConstants.LEFT);
			lblResponseType.setForeground(Color.BLACK);
			lblResponseType.setFont(new Font("David", Font.BOLD, 20));
			lblResponseType.setBackground(Color.WHITE);
			lblResponseType.setBounds(10, 116, 139, 29);
		}
		return lblResponseType;
	}
	
	JLabel getLblrefusalDescription(){
		
		if(lblrefusalDescription==null){
			lblrefusalDescription=new JLabel("Refusal Description :");
			lblrefusalDescription.setFont(new Font("David", Font.BOLD, 20));
			lblrefusalDescription.setBounds(10,211, 191, 29);
		}
		return lblrefusalDescription;
		
	}
	
JLabel getLblProdID(){
		
		if(lblProdID==null){
			lblProdID=new JLabel("Product ID:");
			lblProdID.setFont(new Font("David", Font.BOLD, 20));
			lblProdID.setBounds(10,156, 191, 29);
		}
		return lblProdID;
		
	}
	
JTextField getTextProdID() {
	if (textProdID == null) {
		textProdID = new JTextField();
		textProdID.setColumns(10);
		textProdID.setBackground(SystemColor.menu);
		textProdID.setBounds(223, 161, 241, 23);
	}
	
	return textProdID ;
}
	JTextField getTextResponseType() {
		if (textResponseType == null) {
			textResponseType = new JTextField();
			textResponseType.setColumns(10);
			textResponseType.setBackground(SystemColor.menu);
			textResponseType.setBounds(223, 117, 241, 29);
		}
		
		return textResponseType ;
	}
	
	JTextField getTextrefusalDescription() {
		if (textrefusalDescription == null) {
			textrefusalDescription = new JTextField();
			textrefusalDescription.setColumns(10);
			textrefusalDescription.setBackground(SystemColor.menu);
			textrefusalDescription.setBounds(223, 207, 241, 39);
		}
		
		return textrefusalDescription ;
	}
	
	JLabel getLblDuration(){
		if(lblDuration==null){
			lblDuration=new JLabel("Duration :");
			lblDuration.setHorizontalAlignment(SwingConstants.LEFT);
			lblDuration.setForeground(Color.BLACK);
			lblDuration.setFont(new Font("David", Font.BOLD, 20));
			lblDuration.setBackground(Color.WHITE);
			lblDuration.setBounds(10, 266, 139, 29);
		}
		return lblDuration;
	}
	JLabel getLblSaleID(){
		if(lblSaleID==null){
			lblSaleID=new JLabel("Sale ID:");
			lblSaleID.setHorizontalAlignment(SwingConstants.LEFT);
			lblSaleID.setForeground(Color.BLACK);
			lblSaleID.setFont(new Font("David", Font.BOLD, 20));
			lblSaleID.setBackground(Color.WHITE);
			lblSaleID.setBounds(10, 81, 139, 29);
		}
		return lblSaleID;
	}
	JLabel getLblCustID(){
		if(lblCustID==null){
			lblCustID=new JLabel("Customer ID:");
			lblCustID.setHorizontalAlignment(SwingConstants.LEFT);
			lblCustID.setForeground(Color.BLACK);
			lblCustID.setFont(new Font("David", Font.BOLD, 20));
			lblCustID.setBackground(Color.WHITE);
			lblCustID.setBounds(10, 50, 139, 29);
		}
		return lblCustID;
	}
	JTextField getTextDuration() {
		if (textDuration == null) {
			textDuration = new JTextField();
			textDuration.setColumns(10);
			textDuration.setBackground(SystemColor.menu);
			textDuration.setBounds(223, 271, 241, 20);
		}
		return textDuration;}
		
	JTextField getTextCustID() {
		if (textCustID == null) {
			textCustID = new JTextField();
			textCustID.setColumns(10);
			textCustID.setBackground(SystemColor.menu);
			textCustID.setBounds(223, 55, 241, 20);
		}
		return textCustID;}
		JTextField getTextSaleID() {
			if (textSaleID == null) {
				textSaleID = new JTextField();
				textSaleID.setColumns(10);
				textSaleID.setBackground(SystemColor.menu);
				textSaleID.setBounds(223, 86, 241, 20);
			}
			
		return textSaleID ;
	}
	private JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(44, 318, 125, 23);
		}
		return btnGoBack;
	}
	/**
	 * This inner class implements event listener for pressing GoBack New 
	 * @author OLA
	 * @author AL
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new EmployeePortal());
		}
	}
	
	JButton getBtnUpdate(){
		if (btnUpdate == null) {
			btnUpdate = new JButton("Update");
			btnUpdate.addActionListener(new BbtnUpdateActionListener());
			btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnUpdate.setBounds(311, 318, 125, 23);
		}
		return btnUpdate;
		
	}
	/**
	 * This inner class implements event listener for pressing update button
	 * @author OLA
	 * @author AL
	 *
	 */
	private class BbtnUpdateActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			String prodID=textProdID.getText();
			String responseType = textResponseType.getText();
			String descr=textrefusalDescription.getText();
			String Duration = textDuration.getText();
			String CustomerID = textCustID.getText();
			String saleID = textSaleID.getText();
			
			if (prodID.isEmpty() || Duration.isEmpty() || responseType.isEmpty() || (descr.isEmpty() && responseType.equals("0"))) throw new NullPointerException();
			CustomerResponse cr= new CustomerResponse(-1, CustomerID, saleID ,responseType, descr, Integer.parseInt(prodID), Duration);
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_RESPONSE, cr);
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
	 	    	   ClientStart.quitApp();
			} catch (NullPointerException e) {
				JOptionPane.showMessageDialog(null, "All fields are mandatory", "Customer Response", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}
